package 博客.原型模式;

import 原型模式.客户类.Customer;

import java.io.*;
import java.security.PublicKey;

//这是Laowang类
public class Laowang implements Serializable{
    private String name;
    private String city;
    private School school;

    public Laowang(String name, String city, School school) {
        this.name = name;
        this.city = city;
        this.school = school;
    }
    //深度克隆
    public Laowang deepClone() throws IOException, ClassNotFoundException {
        ByteArrayOutputStream bao=new ByteArrayOutputStream();
        ObjectOutputStream oos=new ObjectOutputStream(bao);
        oos.writeObject(this);//将当前对象写入对象流

        ByteArrayInputStream bis=new ByteArrayInputStream(bao.toByteArray());//从对象流中取出
        ObjectInputStream ois=new ObjectInputStream(bis);
        return (Laowang) ois.readObject();
    }

    @Override
    protected Object clone() throws CloneNotSupportedException {
        School s1 = (School) school.clone();//让school自己先复制一个对象
        Laowang l1 = (Laowang) super.clone();//让自己也复制一个对象
        l1.setSchool(s1);//把刚才School复制后的对象赋值给l1
        return l1;//返回l1
    }

    public String toString() {
        return "Laowang{" +
                "name='" + name + '\'' +
                ", city='" + city + '\'' +
                ", school='" + school + '\'' +
                '}';
    }
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public School getSchool() {
        return school;
    }

    public void setSchool(School school) {
        this.school = school;
    }
}
